import { _decorator, Component, Label, Node, UI } from 'cc';
import { Ally, AllyOperateType } from '../entity/Ally';
import { UIManager } from '../common/UIManager';
import AllyApi from '../api/AllyApi';
import { EventManager } from '../event/EventManager';
import { PrefabEnum } from '../common/PrefabEnum';
const { ccclass, property } = _decorator;

@ccclass('InviteMeItem')
export class InviteMeItem extends Component {
    @property(Label)
    title: Label
    @property(Label)
    allyName: Label
    @property(Label)
    leader: Label
    @property(Node)
    refuse: Node
    @property(Node)
    agree: Node

    private ally: Ally

    start() {
        this.refuse.on(Node.EventType.TOUCH_END, this.onRefuse, this)
        this.agree.on(Node.EventType.TOUCH_END, this.onAgree, this)
    }

    setData(ally: Ally) {
        this.ally = ally
        this.title.string = `${ally.AllyCutName}  邀请你加入他们的联盟`
        this.allyName.string = ally.AllyCutName + '的联盟'
        this.leader.string = ally.AllyCutName
    }

    async onRefuse() {

        try {
            await UIManager.instance.modal('提示', `您确定拒绝${this.ally.AllyCutName}的联盟吗？`,)
            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.RefuseInvite,
                d: this.ally.InviteId
            })
            UIManager.instance.closeLoading()

            EventManager.instance.emit(EventManager.EventType.RefreshInviteMeList, 1)
        } catch (error) {

        }



    }

    async onAgree() {

        try {
            await UIManager.instance.modal('提示', `您确定加入${this.ally.AllyCutName}的联盟吗？`,)

            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.AcceptInvite,
                d: this.ally.InviteId
            })
            UIManager.instance.closeLoading()

            UIManager.instance.closeUI(PrefabEnum.AllyIndex)
            UIManager.instance.openUI(PrefabEnum.AllyIndex)

        } catch (error) {

        }


    }
}


